package GUI;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Logic.GameEngine;
import Models.Dice;
import Models.Player;
import Tiles.Tile;

public class GUI extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	private List<Tile> tileList;
	private JPanel sidePanel = new JPanel();
	private JPanel board = new JPanel();
	private JLabel randomLabel = new JLabel();
	private JButton rollButton;
	private GameEngine gE;
	private List<Dice> dices;
	private List<JLabel> diceLabelList = new ArrayList<JLabel>();
	private List<Player> playerList;
	private List<JLabel> playerLabelList = new ArrayList<JLabel>();
	private List<JLabel> tileLabelList = new ArrayList<JLabel>();
	
	public GUI(GameEngine gE){
		this.setLayout(null);
		this.gE = gE;
		this.tileList = gE.getTileList();
		this.dices = gE.getDices();
		playerList = gE.getPlayerList();
		this.setSize(800, 600);
		
		board.setLayout(new GridLayout((int) Math.sqrt(gE.getTileList().size()), (int) Math.sqrt(gE.getTileList().size())));
		sidePanel.setLayout(new GridLayout(6, 1));
		
		sidePanel.setBounds(0, 0, 200, 600);
		board.setBounds(200, 0, 600, 600);
		
		for(Dice d : dices){
			diceLabelList.add(new JLabel(d.toString(d.roll())));
		}
		rollButton = new JButton("Roll");
		rollButton.addActionListener(this);
		
		for(JLabel dLabel : diceLabelList){
			sidePanel.add(dLabel);
		}
		sidePanel.add(randomLabel);
		sidePanel.add(rollButton);
		
		for(Tile t : tileList){
			JLabel tLabel = new JLabel(t.getLabel());
			//tLabel.setIcon(t.getLabel());
			board.add(tLabel);
			tileLabelList.add(tLabel);
		}
		
		for(Player p : playerList){
			JLabel pLabel = new JLabel(p.getName());
			sidePanel.add(pLabel);
			playerLabelList.add(pLabel);
		}
		
		sidePanel.setVisible(true);
		board.setVisible(true);
		this.add(sidePanel);
		this.add(board);
		
		this.setVisible(true);
		update();
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	private void update(){
		List<JLabel> tempList = new ArrayList<JLabel>(playerLabelList);
		for(JLabel tempLabel : tempList){
			tempLabel.setFont(new Font(tempLabel.getFont().getName(), Font.PLAIN, tempLabel.getFont().getSize()));
		}
		playerLabelList = tempList;
		JLabel curr = playerLabelList.get(playerList.indexOf(gE.getCurrentPlayer()));
		curr.setFont(new Font(curr.getFont().getName(), Font.BOLD, curr.getFont().getSize()));
		
		List<JLabel> tempTile = new ArrayList<JLabel>(tileLabelList);
		for(JLabel label : tempTile){
			label.setIcon(tileList.get(tileLabelList.indexOf(label)).getLabel());
			//label.setText(tileList.get(tempList.indexOf(label)).getLabel());
		}
		
		for(Player p : playerList){			
			JLabel temp = tempTile.get(tileList.indexOf(p.getTile()));
			temp.setVisible(true);
			temp.setIcon(p.getImage());
		}
		tileLabelList = tempTile;
		
		for(JLabel d : diceLabelList){
			d.setText(dices.get(diceLabelList.indexOf(d)).getEyes() + "" );
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == rollButton){
			gE.rollDice();
			
			if (!gE.isGameEnd())
				update();	
			else {
				rollButton.setEnabled(false);
				JOptionPane.showMessageDialog(this, "Game ended. Winner: "+gE.getWinner());
			}
		}
		
	}
}